<script setup lang="ts">
import { reactive, ref } from 'vue'

const list = ref([
  { id: 1, name: '《红楼梦》', author: '曹雪芹' },
  { id: 2, name: '《西游记》', author: '吴承恩' },
  { id: 3, name: '《水浒传》', author: '施耐庵' },
  { id: 4, name: '《三国演义》', author: '罗贯中' }
])

const Delete = (id: number) => {
  list.value=list.value.filter(item=>item.id!==id)
}

const BookInfo=reactive({
  id:0, 
  name:'',
  author:''
})

const AddBook=()=>{
BookInfo.id=list.value.length+1
list.value.push({...BookInfo})
}
</script>
 
<template>
请输入书名：<input type="text" v-model="BookInfo.name"><br><br>
请输入作者：<input type="text" v-model="BookInfo.author"><br><br>
<p>{{ BookInfo }}</p>
<button @click="AddBook">添加</button>
  
  <div id="app">
    <h3>黑马书架</h3>
    <ol>
      <li v-for="item in list " :key="item.id">
        <span>{{ item.name }}</span>
        <span>{{ item.author }}</span>
        <button @click="Delete(item.id)">删除</button>
      </li>
    </ol>
  </div>
</template>